package dotsandboxs;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

/**
 * ChessGUI.java 继承JPanel 点格棋的界面类
 */
public class ChessGUI extends JPanel {
    static ArrayList<Status> list = new ArrayList<Status>();

    static int[] block = new int[25];
    static int[] redBox = new int[25];
    static int[] blueBox = new int[25];

    static int[] side = new int[60];
    static int[] redSide = new int[60];
    static int[] blueSide = new int[60];


    static int redBlock = 0;
    static int blueBlock = 0;

    static int color = 0;// 0表示红色，1表示蓝色
    static int count = 0;// 点击坐标点数目的标志;            用于切换下棋方

    static int sideCount = 0;// 边下标计数器
    static int redSideCount = 0;// 红边下标计数器
    static int blueSideCount = 0;// 蓝边下标计数器

    static int boxCount = 0;// 格子下标计数器
    static int redBoxCount = 0;// 红格子下标计数器
    static int blueBoxCount = 0;// 蓝格子下标计数器

    static final int INITHEIGHT = 21;
    static final int INITWIDTH = 21;

    static int gameMode = 0; //是否已经开始游戏的标志

    JFrame frame;
    Graphics g;
    JMenuBar menuBar;
    JMenu menuFile;
    JMenu menuChoice;
    JMenuItem menuItemOpen;
    JMenuItem menuItemSave;
    JMenuItem menuItemStart;
    JMenuItem menuItemRevoke;
    JMenuItem menuItemClear;

    /**
     * 界面类无参构造方法
     */
    public ChessGUI() {
        // 组件实例化
        frame = new JFrame("点格棋  @小分队3");
        frame.setSize(600, 600);
        menuBar = new JMenuBar();
        menuFile = new JMenu("文件F");
        menuFile.setMnemonic('F');
        menuChoice = new JMenu("选项C");
        menuChoice.setMnemonic('C');
        menuItemOpen = new JMenuItem("打开O", 'O');
        menuItemSave = new JMenuItem("保存S", 'S');
        menuItemClear = new JMenuItem("清空C", 'C');
        menuItemStart = new JMenuItem("开始B", 'B');
        menuItemRevoke = new JMenuItem("撤销R", 'R');
        // 组件组装
        frame.setJMenuBar(menuBar);
        menuBar.add(menuFile);
        menuBar.add(menuChoice);
        menuFile.add(menuItemOpen);
        menuFile.add(menuItemSave);
        menuFile.add(menuItemClear);
        menuChoice.add(menuItemStart);
        menuChoice.add(menuItemRevoke);
        frame.add(this);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        g = frame.getContentPane().getGraphics();
        this.paint(g);
        // 注册监听器
        this.addMouseListener(new Monitor(this));
        menuItemOpen.addActionListener(new MenuMonitor(this));
        menuItemSave.addActionListener(new MenuMonitor(this));
        menuItemStart.addActionListener(new MenuMonitor(this));
        menuItemRevoke.addActionListener(new MenuMonitor(this));
        menuItemClear.addActionListener(new MenuMonitor(this));
    }

    /**
     * 重现paint方法，实现重绘棋盘，重绘边，重绘格子
     *
     * @param g 绘图类对象
     */
    public void paint(Graphics g) {
        // 画棋盘
        Graphics2D g2d = (Graphics2D) g;
        g2d.setStroke(new BasicStroke(2.2f)); // 线条变粗
        // 分别画横竖6条线
        for (int line = 0; line < 6; line++) {
            g.setColor(Color.gray);
            g.drawLine(20, 20 + line * 100, 520, 20 + 100 * line);
            g.drawLine(20 + 100 * line, 20, 20 + 100 * line, 520);
        }
        // 画36个黑色圆点
        for (int row = 0; row < 6; row++) {
            for (int col = 0; col < 6; col++) {
                g.setColor(Color.black);
                g.fillOval(15 + 100 * col, 15 + row * 100, 10, 10);
            }

        }
        // 画走过的线
        for (int redSideIndex = 0; redSideIndex < redSide.length; redSideIndex++) {
            if (redSide[redSideIndex] != 0) {
                int xf = (redSide[redSideIndex] / 100) / 10;
                int yf = (redSide[redSideIndex] / 100) % 10;
                int xl = (redSide[redSideIndex] % 100) / 10;
                int yl = (redSide[redSideIndex] % 100) % 10;
                g2d.setStroke(new BasicStroke(5.5f));
                g.setColor(Color.red);
                g.drawLine(xf * 100 + ChessGUI.INITWIDTH, yf * 100 + ChessGUI.INITHEIGHT,
                        xl * 100 + ChessGUI.INITWIDTH, yl * 100 + ChessGUI.INITHEIGHT);
            }
        }

        for (int blueSideIndex = 0; blueSideIndex < blueSide.length; blueSideIndex++) {
            if (blueSide[blueSideIndex] != 0) {
                int xf = (blueSide[blueSideIndex] / 100) / 10;
                int yf = (blueSide[blueSideIndex] / 100) % 10;
                int xl = (blueSide[blueSideIndex] % 100) / 10;
                int yl = (blueSide[blueSideIndex] % 100) % 10;
                g2d.setStroke(new BasicStroke(5.5f));
                g.setColor(Color.blue);
                g.drawLine(xf * 100 + ChessGUI.INITWIDTH, yf * 100 + ChessGUI.INITHEIGHT,
                        xl * 100 + ChessGUI.INITWIDTH, yl * 100 + ChessGUI.INITHEIGHT);
            }
        }

        // 画已经被占据的格子
        for (int redBoxIndex = 0; redBoxIndex < redBox.length; redBoxIndex++) {
            g.setColor(Color.red);
            if (redBox[redBoxIndex] != 0) {
                // 等于 -1 说明占据的是第一个格子
                if (redBox[redBoxIndex] == -1) {
                    g.fillRect(ChessGUI.INITWIDTH + ((redBox[redBoxIndex] + 1) % 5) * 100,
                            ChessGUI.INITHEIGHT + ((redBox[redBoxIndex] + 1) / 5) * 100, 100, 100);
                } else {
                    g.fillRect(ChessGUI.INITWIDTH + (redBox[redBoxIndex] % 5) * 100,
                            ChessGUI.INITHEIGHT + (redBox[redBoxIndex] / 5) * 100, 100, 100);
                }

            }
        }

        for (int blueBoxIndex = 0; blueBoxIndex < blueBox.length; blueBoxIndex++) {
            g.setColor(Color.blue);
            if (blueBox[blueBoxIndex] != 0) {
                // 等于 -1 说明占据的是第一个格子
                if (blueBox[blueBoxIndex] == -1) {
                    g.fillRect(ChessGUI.INITWIDTH + ((blueBox[blueBoxIndex] + 1) % 5) * 100,
                            ChessGUI.INITHEIGHT + ((blueBox[blueBoxIndex] + 1) / 5) * 100, 100, 100);
                } else {
                    g.fillRect(ChessGUI.INITWIDTH + (blueBox[blueBoxIndex] % 5) * 100,
                            ChessGUI.INITHEIGHT + (blueBox[blueBoxIndex] / 5) * 100, 100, 100);
                }

            }
        }
    }
}
